package com.chenshu.intercept_demo.controller;

import com.chenshu.intercept_demo.model.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-21
 * Time：16:50
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String username, String password) {
        //1. 非空判断，这里直接使用Spring提供的工具方法来实现
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            //2. 验证密码是否正确
            if ("admin".equals(username) && "admin".equals(password)) {
                HttpSession session = request.getSession(true);
                session.setAttribute("user", "admin");
                return "登录成功！";
            } else {
                return "用户名或密码输入错误，登录失败！";
            }
        }
        return "用户名或密码为空，请重新输入！";
    }

    @RequestMapping("/reg")
    public String reg() {
        Object obj = null;
        obj.hashCode();
        return "用户进行注册操作！";
    }

    @RequestMapping("/getall")
    public String getAll() {
        return "查询用户信息成功！";
    }

    @RequestMapping("/getone")
    public User getOne() {
        User user = new User();
        user.setUsername("zhangsan");
        user.setPassword("123");
        return user;
    }
}
